package com.gpsbuddy.navigation;
/*
 * OptionsMenu.fx
 *
 * Created on 7.2.2010, 12:35:
 */

import javafx.scene.Group;
import javafx.scene.shape.Rectangle;
import javafx.scene.paint.Color;
import com.gpsbuddy.components.CustomTextBox;
import javafx.scene.text.Text;
import com.gpsbuddy.components.CustomPasswordBox;
import javafx.scene.control.Button;
import javafx.scene.layout.VBox;
import javafx.geometry.HPos;
import com.gpsbuddy.utils.DatabaseUtils;
import javafx.scene.Scene;
import com.gpsbuddy.main.Enviroment;
import javafx.animation.Interpolator;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import com.gpsbuddy.data.Property;
import javafx.scene.control.CheckBox;
import com.gpsbuddy.utils.ServiceUtils;
import com.gpsbuddy.services.User;

/**
 * @author jonycus
 */
public class ServiceOptionsPage extends Scene {

    public-init var env: Enviroment;
    public var mainBarWidth: Number;
    public var mainBarHeight: Number;
    public var title: String;
    var titleComp = Text {
                fill: Color.BLACK;
                content: bind title;
                layoutX: width / 2 - (title.length() * 3);
                layoutY: 20;
            }
    var background = Rectangle {
                fill: Color.WHITE;
                opacity: 0.9;
                width: bind env.screenWidth;
                height: bind env.screenHeight;
            }
    var inputName = CustomTextBox {
                env: env;
                layoutX: 15;
                layoutY: 40;
                label: "Name:"
            }
    var inputSurname = CustomTextBox {
                env: env;
                layoutX: 15;
                layoutY: 40;
                label: "Surname:"
            }
    var inputEmail = CustomTextBox {
                env: env;
                layoutX: 15;
                layoutY: 40;
                label: "Email:"
            }
    var inputUserName = CustomTextBox {
                env: env;
                layoutX: 15;
                layoutY: 40;
                label: "Username:"
            }
    var inputPassword = CustomPasswordBox {
                env: env;
                layoutX: 15;
                layoutY: 70;
                label: "Password: "
            }
    var connectButton = Button {
                text: "OK";
                action: okButtonFunction;
            }
    var registerButton = Button {
                text: "Register";
                action: registerToService;
            }
    var saveInfoCheckBox = CheckBox {
                selected: false;
                text: "Save info?"
            }
    var contentBox = VBox {
                layoutX: 20;
                layoutY: 30;
                nodeHPos: HPos.CENTER;
                spacing: 10;
                content: [inputName, inputSurname, inputEmail, inputUserName, inputPassword, saveInfoCheckBox, connectButton, registerButton]
            }
    var mainNavigationBar = MainStatusBar {
                env: env;
                width: bind mainBarWidth;
                height: bind mainBarHeight;
                barHeight: 30
            }
    var optionsContent = Group {
                layoutX: bind lX;
                opacity: bind op;
                content: [background, titleComp, contentBox, mainNavigationBar]
            }

    postinit {
        if (DatabaseUtils.initialized) {
            fillStoredFields();
        }
        content = [optionsContent];
    }

    function okButtonFunction() {
        ServiceUtils.loginToService(inputUserName.text, inputPassword.text);
        if (saveInfoCheckBox.selected) {
            store();
            }
    }

    function registerToService() {
        var user: User = User {
                    username: inputUserName.text;
                    password: inputPassword.text;
                    name: inputName.text;
                    surname: inputSurname.text;
                    email: inputEmail.text;
                }
            ServiceUtils.registerToService(user);
            ServiceUtils.loginToService(inputUserName.text, inputPassword.text);
            if (saveInfoCheckBox.selected) {
                store();
            }
    }

    public function store(): Void {
        if (DatabaseUtils.hasProp("username")) {
            DatabaseUtils.updateProp("username", inputUserName.text);
        } else {
            DatabaseUtils.addProp("username", inputUserName.text);
        }
        if (DatabaseUtils.hasProp("password")) {
            DatabaseUtils.updateProp("password", inputPassword.text);
        } else {
            DatabaseUtils.addProp("password", inputPassword.text);
        }
        if (DatabaseUtils.hasProp("name")) {
            DatabaseUtils.updateProp("name", inputName.text);
        } else {
            DatabaseUtils.addProp("name", inputName.text);
        }
        if (DatabaseUtils.hasProp("surname")) {
            DatabaseUtils.updateProp("surname", inputSurname.text);
        } else {
            DatabaseUtils.addProp("surname", inputSurname.text);
        }
        if (DatabaseUtils.hasProp("email")) {
            DatabaseUtils.updateProp("email", inputEmail.text);
        } else {
            DatabaseUtils.addProp("email", inputEmail.text);
        }
        if (DatabaseUtils.hasProp("saveInfo")) {
            DatabaseUtils.updateProp("saveInfo", saveInfoCheckBox.selected.toString());
        } else {
            DatabaseUtils.addProp("saveInfo", saveInfoCheckBox.selected.toString());
        }
        DatabaseUtils.storeData();
        DatabaseUtils.printData();
    }

    var op: Number = 0;
    var lX: Number = -env.screenWidth;

    public function transitIn() {
        Timeline {
            keyFrames: [
                KeyFrame {
                    time: 0s
                    values: [lX => -env.screenWidth tween Interpolator.LINEAR, op => 0.0 tween Interpolator.LINEAR];
                }
                KeyFrame {
                    time: 0.5s
                    values: [lX => 0 tween Interpolator.LINEAR, op => 1.0 tween Interpolator.LINEAR];
                }
            ]
        }.play();
    }

    function fillStoredFields() {
        for (prop: Property in DatabaseUtils.props) {
            if (prop.name.equals("name")) {
                inputName.text = prop.value;
            }
            if (prop.name.equals("surname")) {
                inputSurname.text = prop.value;
            }
            if (prop.name.equals("email")) {
                inputEmail.text = prop.value;
            }
            if (prop.name.equals("username")) {
                inputUserName.text = prop.value;
            }
            if (prop.name.equals("password")) {
                inputPassword.text = prop.value;
            }
            if (prop.name.equals("saveInfo")) {
                saveInfoCheckBox.selected = prop.value.equals("true");
            }
        }
    }

}
